﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Runtime.InteropServices

Namespace System.Workflow.Runtime
    <DefaultMember("Item")> _
    Friend Class [Set]([Of] T As IComparable)
        Implements IEnumerable([Of] T), IEnumerable
        ' Methods
        Public Sub New()
            Me.list = New List([Of] T)
        End Sub

        Public Sub New(ByVal capacity As Integer)
            Me.list = New List([Of] T)(capacity)
        End Sub

        Public Sub Add(ByVal item As T)
            Dim insertPos As Integer = -1
            If Me.Search(item, insertPos) Then
                Throw New ArgumentException(ExecutionStringManager.ItemAlreadyExist)
            End If
            Me.list.Insert(insertPos, item)
        End Sub

        Public Function Contains(ByVal item As T) As Boolean
            Dim insertPos As Integer = -1
            Return Me.Search(item, insertPos)
        End Function

        Public Function GetEnumerator() As IEnumerator([Of] T)
            Return Me.list.GetEnumerator
        End Function

        Private Function Search(ByVal item As T, <Out> ByRef insertPos As Integer) As Boolean
            insertPos = -1
            Dim num As Integer = 0
            Dim count As Integer = Me.list.Count
            Dim num3 As Integer = -1
            Dim num4 As Integer = 0
            Do While ((count - num3) > 1)
                num = ((count + num3) / 2)
                num4 = Me.list.Item(num).CompareTo(item)
                If (num4 = 0) Then
                    insertPos = num
                    Return True
                End If
                If (num4 > 0) Then
                    count = num
                Else
                    num3 = num
                End If
            Loop
            If (num3 = -1) Then
                insertPos = 0
                Return False
            End If
            If (num4 <> 0) Then
                insertPos = If((num4 < 0), (num + 1), num)
                Return False
            End If
            Return True
        End Function

        Private Function System.Collections.IEnumerable.GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
            Return Me.list.GetEnumerator
        End Function

        Public Function TryAdd(ByVal item As T) As Boolean
            Dim insertPos As Integer = -1
            If Not Me.Search(item, insertPos) Then
                Me.list.Insert(insertPos, item)
                Return True
            End If
            Return False
        End Function

        Public Function TryGetValue(ByVal item As T, <Out> ByRef value As T) As Boolean
            Dim insertPos As Integer = -1
            If Me.Search(item, insertPos) Then
                value = Me.list.Item(insertPos)
                Return True
            End If
            value = CType(Nothing, T)
            Return False
        End Function


        ' Properties
        Public ReadOnly Property Count As Integer
            Get
                Return Me.list.Count
            End Get
        End Property

        Public ReadOnly Default Property Item(ByVal index As Integer) As T
            Get
                Return Me.list.Item(index)
            End Get
        End Property


        ' Fields
        Private list As List([Of] T)
    End Class
End Namespace

